package com.codename1.samples;


import com.codename1.components.SpanLabel;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.CN;
import static com.codename1.ui.ComponentSelector.$;
import com.codename1.ui.Container;
import com.codename1.ui.TextArea;
import com.codename1.ui.TextField;
import com.codename1.ui.TextSelection;
import com.codename1.ui.plaf.Border;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class TextSelectionSample {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        hi.setScrollableY(false);
        TextSelection sel = hi.getTextSelection();
        sel.addTextSelectionListener(e->{
            //System.out.println("Text selection has changed");
        });
        
        sel.setEnabled(true);
        Label label = new Label("This label should be selectable");
        label.setTextSelectionEnabled(true);
        Label label2 = new Label("Some more text");
        label2.setTextSelectionEnabled(true);
        hi.add(label);
        hi.add(new TextField("Hello Universe"));
        hi.add(label2);
        hi.add(new Label("Hi World"));
        
        Container cnt = new Container(BoxLayout.x());
        cnt.setScrollableX(true);
        cnt.getStyle().setBorder(Border.createLineBorder(1, 0x0));
        cnt.setPreferredH(CN.convertToPixels(5));
        cnt.setPreferredW(CN.convertToPixels(20));
        
        TextArea ta = new TextArea();
        ta.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.");
        ta.setEnabled(false);
        ta.setRows(6);
        hi.add(ta);
        
        SpanLabel sl = new SpanLabel();
        sl.setText(ta.getText());
        sl.setTextSelectionEnabled(true);
        hi.add(sl);
        
        
        TextField tf = new TextField();
        tf.setText("Hello World.  This is a test field");
        tf.setEnabled(false);
        hi.add(tf);
        
        Label l = new Label("This is a test with some long text to see if this works.  It should just flow when it runs out of space");
        l.setTextSelectionEnabled(true);
        cnt.add(l);
        
        Container cntY = new Container(BoxLayout.y());
        cntY.setScrollableY(true);
        cntY.getStyle().setBorder(Border.createLineBorder(1, 0x0));
        for (int i=0; i<50; i++) {
            Label li = new Label("List item "+i);
            li.setTextSelectionEnabled(true);
            cntY.add(li);
        }
        hi.add(cnt);
        hi.add(cntY);
        
        $(cnt, cntY).selectAllStyles().setMarginMillimeters(4);
        
        
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
